package com.winchain.chainbill.risk.base.service.impl;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.base.utils.SystemUtil.SystemMode;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.winchain.chainbill.receivables.base.dao.ArRecordDao;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.risk.base.dao.RmAccessDao;
import com.winchain.chainbill.risk.base.po.RmAccess;
import com.winchain.chainbill.risk.base.service.RmAccService;
import com.winchain.chainbill.risk.biz.access.vo.AccessVo;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("rmAccService")
public class RmAccServiceImpl extends PersistentServiceImpl<RmAccess> implements RmAccService {
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private ArRecordDao arRecordDao;
	@Autowired
	private RmAccessDao rmAccessDao;

	public RmAccess getAccessByBuyerAndSellerId(String buyerId, String sellerId) {
		RmAccess entity = new RmAccess();
		entity.setBuyerId(buyerId);
		entity.setSellerId(sellerId);
		return (RmAccess) selectOne(entity);
	}

	@Pagination
	public List<AccessVo> selectAccessForPage(PageLimit limit, RmAccess rmAccess) {
		return this.rmAccessDao.selectAccessForPage(rmAccess);
	}

	public List<ArRecord> getArRecords(String sellerId, List<String> buyerIds) {
		String[] arStates = { "0", "1", "2", "3", "4" };

		List<ArRecord> list = this.arRecordDao.selectRecordBySellerIdAndBuyerIds(sellerId, buyerIds, arStates);

		return list;
	}

	public boolean checkAccessIsExcess(RmAccess mRmAccess, RmAccess accessVo) {
		if ((("1".equals(mRmAccess.getOutputAvl())) || ("1".equals(mRmAccess.getLoanAvl())))
				&& ("0".equals(accessVo.getOutputAvl())) && ("0".equals(accessVo.getLoanAvl()))) {
			return true;
		}
		return false;
	}

	public boolean computeCreditMoney(List<ArRecord> list, String sellerId) {
		if ((list != null) && (list.size() > 0)) {
			String finId = CommonDataUtil.getCurEntId();
			BigDecimal creditMoney = getCreditMoney(sellerId, finId);

			BigDecimal nowCreditMoney = new BigDecimal(0.0D);
			for (ArRecord arRecord : list) {
				nowCreditMoney = nowCreditMoney.add(arRecord.getCreditMoney());
			}
			if ((!SystemUtil.SystemMode.dev.equals(SystemUtil.getSystemMode()))
					&& (creditMoney.compareTo(nowCreditMoney) < 0)) {
				return true;
			}
		}
		return false;
	}

	private BigDecimal getCreditMoney(String sellerId, String fiNo) {
		AcctBas AcctBas = this.billAcctService.getAcctBas(sellerId, "1", null);
		return AcctBas.getAcctBlc();
	}
}
